//
//  MallClassifyViewController.m
//  xiaobaima
//
//  Created by xbmios on 2018/1/19.
//  Copyright © 2018年 xbmios. All rights reserved.
//

#import "MallClassifyViewController.h"
#import "MallClassifyContentViewController.h"
//#import "UILabel+Extension.h"

#import "SupportCategoryModel.h"


@interface MallClassifyViewController () <UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView *titleScrollView;

@property (strong, nonatomic) UIScrollView *contentScrollView;

@property (strong, nonatomic) NSMutableArray *titleArr;

@property (assign, nonatomic) NSInteger numbers;

@property (copy, nonatomic) NSString *str;
@property (copy, nonatomic) NSString *str1;


@property (strong, nonatomic) NSMutableArray *dictArr;

@end

@implementation MallClassifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = Colorf2f2f2;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"商城分类";
    
    self.numbers = 0;

    // 商城分类接口
    [self loadCategoryTitles];

    [self setupUI];

}

- (void)loadCategoryTitles {

    [SupportCategoryModel supportCategoryModelwithSuccess:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            NSArray *arr = responseObject[@"data"];
            
            self.titleArr = [NSMutableArray arrayWithArray:arr];
            
            // 设置标题scrollview
            [self setUPtitleScrollView];
            [self setupChildVCs];
            
        }else {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
        }
        
    } fail:^(NSError *error) {
        
        if (error) {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
        }
        
    }];
    
}

#pragma mark -标题scrollview label点击事件
- (void)titleClickGesture:(UIGestureRecognizer *)recognizer {
 
    NSLog(@"标题label点击事件");
    
    UILabel *label = (UILabel *)recognizer.view;

    CGFloat offsetX = label.tag * self.contentScrollView.frame.size.width;
    
    MallClassifyContentViewController *contentVC = self.childViewControllers[label.tag];
    
    
    SupportCategoryModel *model = self.titleArr[label.tag];
    contentVC.type = model.classId;
    
    
//    if (label.tag > 2) {
    
//        if ([label.classID integerValue] == [self.classID1 integerValue]) {
//
//            label.backgroundColor = Color176ec7;
//
////            offsetx = titleLabel.center.x - self.titleScrollView.width / 2.0;
//
//            contentVC.type = self.classID1;
//            contentVC.classURL = self.str;
//        }else {
//
//            label.backgroundColor = color1e89eb;
//        }
        
//        NSInteger index = label.tag - 3;
//        NSDictionary *dcit = self.titleArr[index];
//        if (dcit[@"id"]) {
//
//            contentVC.type = dcit[@"id"];
//            contentVC.classURL = self.str;
//            contentVC.type1 = 3;
//        }
        
//        contentVC.type = label.classID;
//        contentVC.classURL = self.str;
//        contentVC.type1 = 1;
    
//    }else {
    
//        if ([label.classID integerValue] == [self.classID integerValue]) {
//
//            label.backgroundColor = Color176ec7;
//            contentVC.type = self.classID;
//            contentVC.classURL = self.str1;
//
////            offsetx = titleLabel.center.x - self.titleScrollView.width / 2.0;
//
//        }else {
//            label.backgroundColor = color1e89eb;
//
//        }
        
//        contentVC.type = label.classID;
//        contentVC.classURL = self.str1;
//        contentVC.type1 = 2;
        
//        contentVC.type = [NSString stringWithFormat:@"%zd", label.tag + 1];
//        contentVC.classURL = self.str1;
//        contentVC.type1 = 2;
        
//    }
    
    [self.contentScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    
    for (int i = 0; i < self.childViewControllers.count; i++) {
        
        UILabel *temp = self.titleScrollView.subviews[i] ;
        
        temp.backgroundColor = Color1f89ff;
        
    }
    
    label.backgroundColor = Color176ec7;
    
}

#pragma mark -scrollview 代理方法
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    
    // 获得当前需要显示的子控制器索引
    NSUInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    MallClassifyContentViewController *vc = self.childViewControllers[index];
    
    // 滚动标题栏 (self.titleScrollview)
    UILabel *label = self.titleScrollView.subviews[index];
    for (int i = 0; i < self.childViewControllers.count; i++) {
        
        UILabel *temp = self.titleScrollView.subviews[i] ;
        
        temp.backgroundColor = Color1f89ff;
        
    }
    
    label.backgroundColor = Color176ec7;
    CGFloat width = self.titleScrollView.frame.size.width;
    CGFloat offsetX = label.center.x - width * 0.5;
    
    // 最大偏移量
    CGFloat maxOffsetx = self.titleScrollView.contentSize.width - width;
    if (offsetX < 0) {
        
        offsetX = 0;
        
    } else if (offsetX > maxOffsetx) {
        
        offsetX = maxOffsetx;
        
    }
    
    [self.titleScrollView setContentOffset:CGPointMake(offsetX, 0) animated:NO];
    
    // 如果子控制器的view已经在上面,就直接返回
    if (vc.view.superview) {
    
        return ;
    }
    
    // 添加
    CGFloat vcW = scrollView.frame.size.width;
    CGFloat vcH = scrollView.frame.size.height;
    CGFloat vcY = 0;
    CGFloat vcX = index * vcW;
    vc.view.frame = CGRectMake(vcX, vcY, vcW, vcH);
    [scrollView addSubview:vc.view];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//    [self scrollViewDidEndScrollingAnimation:scrollView];
}


- (void)setupUI {
    
    [self.view addSubview:self.titleScrollView];
    [self.view addSubview:self.contentScrollView];
    
    __weak typeof(self) weakSelf = self;
    [self.contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.titleScrollView.mas_bottom);
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom);
   
    }];
    
}

- (void)setUPtitleScrollView {
    
    CGFloat width = jisuan(150);
    CGFloat height = jisuan(88);
    
    CGFloat offsetx = 0;
    
    for (int i = 0 ; i < self.titleArr.count; i ++) {
        
        SupportCategoryModel *dict = self.titleArr[i];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        
        titleLabel.text = dict.name;
        titleLabel.textColor = WhiteColor;
        titleLabel.font = Font(24);
        titleLabel.tag = i;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.userInteractionEnabled = YES;
        titleLabel.frame = CGRectMake(i * width, 0, width, height);
        [self.titleScrollView insertSubview:titleLabel atIndex:i];

        if ([dict.classId isEqualToString:self.classID]) {
           
            titleLabel.backgroundColor = Color176ec7;
            
            offsetx = titleLabel.center.x - self.titleScrollView.width / 2.0;
            
            
        }else {
            
            titleLabel.backgroundColor = Color1f89ff;
            
        }
        
        UITapGestureRecognizer *titleGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleClickGesture:)];
        [titleLabel addGestureRecognizer:titleGesture];
        
    }
    
    
    // 最大偏移量
    self.titleScrollView.contentSize = CGSizeMake(width * self.titleArr.count, 0);
    
    CGFloat width1 = self.titleScrollView.frame.size.width;
    CGFloat maxOffsetx = self.titleScrollView.contentSize.width - width1;
    
    if (offsetx < 0) {
        
        offsetx = 0;
        
    } else if (offsetx > maxOffsetx) {
        
        offsetx = maxOffsetx;
        
    }
    
    [self.titleScrollView setContentOffset:CGPointMake(offsetx, 0) animated:YES];
    
}

- (void)setupChildVCs {
    
    for (int i = 0; i < self.titleArr.count; i ++) {
    
        MallClassifyContentViewController *contentVC = [[MallClassifyContentViewController alloc] init];
        
        SupportCategoryModel *dict = self.titleArr[i];
        
//        UILabel *label = self.titleScrollView.subviews[i];
        
        if ([dict.classId isEqualToString:self.classID]) {
            
            contentVC.type = self.classID;
//            contentVC.type = self.classID1;
//            contentVC.classURL = self.str;
            
//            contentVC.view.frame = CGRectMake(WIDTH * [self.number integerValue], 0, WIDTH, self.contentScrollView.height);
            contentVC.view.frame = CGRectMake(WIDTH * i, 0, WIDTH, self.contentScrollView.height);
            
            [self.contentScrollView addSubview:contentVC.view];
            
        }

        [self addChildViewController:contentVC];
        
    }
    
//    self.contentScrollView.contentOffset = CGPointMake(WIDTH * [self.number integerValue], 0);
    self.contentScrollView.contentOffset = CGPointMake(WIDTH * self.number, 0);
//
    self.contentScrollView.contentSize = CGSizeMake(WIDTH * self.titleArr.count, 0);

}


- (UIScrollView *)titleScrollView {
    
    if (_titleScrollView == nil) {
        
        _titleScrollView = [[UIScrollView alloc] init];
        _titleScrollView.frame = CGRectMake(0, 0, WIDTH, jisuan(88));
        _titleScrollView.backgroundColor = Color1f89ff;
        _titleScrollView.showsHorizontalScrollIndicator = NO;
        _titleScrollView.showsVerticalScrollIndicator = NO;
        
    }
    
    return _titleScrollView;
}

- (UIScrollView *)contentScrollView {
    
    if (_contentScrollView == nil) {
        
        _contentScrollView = [[UIScrollView alloc] init];
        _contentScrollView.delegate = self;
        _contentScrollView.backgroundColor = WhiteColor;
        _contentScrollView.showsVerticalScrollIndicator = NO;
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        _contentScrollView.pagingEnabled = YES;
        _contentScrollView.bounces = NO;
        _contentScrollView.scrollEnabled = NO;
        _contentScrollView.frame = CGRectMake(0, jisuan(88), WIDTH, HEIGHT - jisuan(88));
        
    }
    
    return _contentScrollView;
}

- (NSMutableArray *)titleArr {
    
    if (_titleArr == nil) {

        _titleArr = [NSMutableArray array];

    }
    
    return _titleArr;
}


- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;

//    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
//    self.navigationController.navigationBar.hidden = NO;
//
//    
//    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
